//
//  GPTaskView.m
//  GoldPig
//
//  Created by 虎爷 on 2020/1/2.
//  Copyright © 2020 虎爷. All rights reserved.
//

#import "GPTaskView.h"
#import "GPEachTaskCell.h"
#import "GPTipView.h"
@import AudioToolbox;

@interface GPTaskView ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIView *signBgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *signHeight_layout;
@property (nonatomic ,strong) UITableView *tableView;

@property (weak, nonatomic) IBOutlet UILabel *serial_sign_1_Lab;
@property (weak, nonatomic) IBOutlet UILabel *serial_sign_2_Lab;
@property (weak, nonatomic) IBOutlet UILabel *serial_sign_3_Lab;
@property (weak, nonatomic) IBOutlet UILabel *serial_sign_4_Lab;
@property (weak, nonatomic) IBOutlet UILabel *serial_sign_5_Lab;
@property (weak, nonatomic) IBOutlet UILabel *serial_sign_6_Lab;
@property (weak, nonatomic) IBOutlet UILabel *serial_sign_7_Lab;


@property (nonatomic, strong)NSNumber *currentIsSign;
@property (nonatomic, strong)NSNumber *serialSignDay;

@property (nonatomic, strong)NSArray *dataArr;
@property (assign, nonatomic) SystemSoundID foldSound;
@end
@implementation GPTaskView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self tableView];
    
    self.dataArr = [[NSArray alloc]init];
    [self serialSignQueryRequest];
    [self taskBonusesSearchResquest];
    [self configureSounds];
}

-(void)taskBonusesSearchResquest{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:@"type"];
    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(taskBonusesSearch) params:dic  success:^(NSInteger code, id  _Nonnull result) {

        if (code == 1) {
            self.dataArr = result[@"data"];
            [self.tableView reloadData];
            
        }else{
            NSString *msg = result[@"msg"];
            [SVProgressHUD showImage:NULL_IMAGE status:msg];
        }

    } fail:^(NSError * _Nonnull error) {

    }];
}


-(void)serialSignQueryRequest{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1926],@"1",[NSNumber numberWithInt:1927],@"2",[NSNumber numberWithInt:1928],@"3",[NSNumber numberWithInt:1929],@"4",[NSNumber numberWithInt:1930],@"5",[NSNumber numberWithInt:1931],@"6",[NSNumber numberWithInt:1932],@"7", nil];
    
    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(serialSignQuery) params:dic success:^(NSInteger code, id  _Nonnull result) {
        //NSLog(@"%@",result);
        
        if (code == 1) {
           NSArray *data = result[@"data"];
           NSDictionary *dic = data[0];
            self.currentIsSign = dic[@"currentIsSign"];
            self.serialSignDay = dic[@"serialSignDay"];
    
            NSString *signData = dic[@"signData"];
            NSDictionary *signDataDic = [self convertjsonStringToDict:signData];
            [self setSignData:signDataDic];
            
            
            if (![self.currentIsSign boolValue]) {
                
            }else{
//               [SVProgressHUD showImage:NULL_IMAGE status:@"今日已签到"];
            }
            
        }
        
        
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(void)serialSignRequest{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1926],@"1",[NSNumber numberWithInt:1927],@"2",[NSNumber numberWithInt:1928],@"3",[NSNumber numberWithInt:1929],@"4",[NSNumber numberWithInt:1930],@"5",[NSNumber numberWithInt:1931],@"6",[NSNumber numberWithInt:1932],@"7", nil];
    
    [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(serialSign) params:dic success:^(NSInteger code, id  _Nonnull result) {
        //NSLog(@"%@",result);
        
        if (code == 1) {
            NSArray *data = result[@"data"];
            NSDictionary *dic = data[0];
            NSNumber *amount = dic[@"amount"];
            self.serialSignDay = dic[@"serialSignDay"];
            
            GPTipView *tipView = [GPTipView initGPTipView];
            tipView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            [self.window addSubview:tipView];
            tipView.amount = amount;
            tipView.name = @"金币";
        }else{
            NSString *msg = result[@"msg"];
            [SVProgressHUD showImage:NULL_IMAGE status:msg];
        }
   
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSDictionary *)convertjsonStringToDict:(NSString *)jsonString{

    NSDictionary *retDict = nil;
    if ([jsonString isKindOfClass:[NSString class]]) {
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        retDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:NULL];
        return  retDict;
    }else{
        return retDict;
    }

}


#pragma mark UI
- (void)setSignData:(NSDictionary *)signData{
    self.serial_sign_1_Lab.text = [NSString stringWithFormat:@"+%@",signData[@"serial_sign_1"]];
    self.serial_sign_2_Lab.text = [NSString stringWithFormat:@"+%@",signData[@"serial_sign_2"]];
    self.serial_sign_3_Lab.text = [NSString stringWithFormat:@"+%@",signData[@"serial_sign_3"]];
    self.serial_sign_4_Lab.text = [NSString stringWithFormat:@"+%@",signData[@"serial_sign_4"]];
    self.serial_sign_5_Lab.text = [NSString stringWithFormat:@"+%@",signData[@"serial_sign_5"]];
    self.serial_sign_6_Lab.text = [NSString stringWithFormat:@"+%@",signData[@"serial_sign_6"]];
    self.serial_sign_7_Lab.text = [NSString stringWithFormat:@"+%@",signData[@"serial_sign_7"]];
    
}

- (void)setSerialSignDay:(NSNumber *)serialSignDay{
    _serialSignDay = serialSignDay;
    
    for (int i = 1; i<=serialSignDay.intValue; i++) {
        UILabel *lab = [self viewWithTag:i+10];
        lab.text = @"已签";
        
        UIImageView *img = [self viewWithTag:i+20];
        img.image = [UIImage imageNamed:@"ggs"];
    }
}


+ (instancetype)initGPTaskView{
    return NSbunleloadNibName(@"GPTaskView");
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-30, 427) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.bgView addSubview:_tableView];

    }
    return _tableView;
    
}


#pragma mark TableViewDelegate&DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GPEachTaskCell *cell = [GPEachTaskCell cellWithTableView:tableView];
    cell.dic = self.dataArr[indexPath.row];
    WEAKSELF
    cell.serialSignCilckBlock = ^{
        [weakSelf serialSignRequest];
    };
    cell.getCilckBlock = ^{
        [weakSelf taskBonusesSearchResquest];
    };
    cell.closeCilckBlock = ^{
        [weakSelf removeFromSuperview];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 73;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)configureSounds
{
    // Configure bloom sound
    //
    NSString *foldSoundPath = [[NSBundle mainBundle]pathForResource:@"fold" ofType:@"caf"];
    NSURL *foldSoundURL = [NSURL fileURLWithPath:foldSoundPath];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)foldSoundURL, &_foldSound);
}
- (IBAction)closeClick:(id)sender {
    AudioServicesPlaySystemSound(self.foldSound);
    [self removeFromSuperview];
}



@end
